local skel = fk.CreateSkill {
  name = "emo__qiming",
  dynamic_desc = function (self, player, lang)
    local used = player:getTableMark("emo__qiming_choice-round")
    local arg1 = table.contains(used, "emo__qiming_judge") and "（已用！）" or ""
    local arg2 = table.contains(used, "emo__qiming_guicai") and "（已用！）" or ""
    local arg3 = player:getMark("emo__qiming_fankui-round") ~= 0 and "未拥有" or "拥有！"
    return "emo__qiming_dyn:".. arg1 .. ":" .. arg2 .. ":" .. arg3
  end,
}

Fk:loadTranslationTable{
  ["emo__qiming"] = "欺命",
  [":emo__qiming"] = "每轮每项限一次，一名角色的准备阶段，你可以选一项：1.令其本回合摸牌阶段改为进行若干次判定直到出现重复花色的判定牌，然后其获得所有判定牌；2.令其获得〖鬼才〗。若你本轮中未对自己发动本技能，你视为拥有〖反馈〗。",

  [":emo__qiming_dyn"] = "每轮每项限一次，一名角色的准备阶段，你可以选一项：1.令其本回合摸牌阶段改为进行若干次判定直到出现重复花色的判定牌，然后其获得所有判定牌{1}；2.令其获得〖鬼才〗{2}。若你本轮中未对自己发动本技能，你视为拥有〖反馈〗（{3}）。",

  ["#emo__qiming-choice"] = "欺命：你可以令 %src 获得一项效果",
  ["emo__qiming_judge"] = "摸牌改为判定并获得判定牌",
  ["emo__qiming_guicai"] = "获得〖鬼才〗",
  ["#emo__qiming-fankui"] = "〖反馈〗：你可以获得 %src 的一张牌",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and not target.dead and data.phase == Player.Start
    and #player:getTableMark("emo__qiming_choice-round") < 2
  end,
  on_cost = function (self, event, target, player, data)
    local all_choices = {"emo__qiming_judge", "emo__qiming_guicai", "Cancel"}
    local choices = table.filter(all_choices, function (choice) return not table.contains(player:getTableMark("emo__qiming_choice-round"), choice) end)
    local choice = player.room:askToChoice(player, {
      choices = choices, all_choices = all_choices, skill_name = skel.name,
      prompt = "#emo__qiming-choice:" .. target.id
    })
    if choice ~= "Cancel" then
      event:setCostData(self, { choice = choice, tos = { target } })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    room:addTableMark(player, "emo__qiming_choice-round", choice)
    if target == player then
      room:setPlayerMark(player, "emo__qiming_fankui-round", 1)
    end
    if choice == "emo__qiming_guicai" then
      room:handleAddLoseSkills(target, "emo__guicai")
    else
      room:setPlayerMark(target, "emo__qiming_judge-turn", 1)
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and not target.dead and data.phase == Player.Draw and not data.phase_end
    and target:getMark("emo__qiming_judge-turn") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.phase_end = true
    local cards = {}
    while not target.dead do
      local parsePattern = table.concat(table.map(cards, function(card)
        return card:getSuitString()
      end), ",")
      local judge = {
        who = target,
        reason = skel.name,
        pattern = ".|.|" .. (parsePattern == "" and "." or "^(" .. parsePattern .. ")"),
        skipDrop = true,
      }
      room:judge(judge)
      table.insert(cards, judge.card)
      if not table.every(cards, function(card) return card == judge.card or judge.card:compareSuitWith(card, true) end) then
        break
      end
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 then
      if target.dead then
        room:cleanProcessingArea(cards)
      else
        room:obtainCard(target, cards, true, fk.ReasonPrey, target, skel.name)
      end
    end
  end,
})

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(skel.name) and player:getMark("emo__qiming_fankui-round") == 0) then return end
    if data.from and not data.from.dead then
      if data.from == player then
        return #player:getCardIds("e") > 0
      else
        return not data.from:isNude()
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__qiming-fankui:" .. data.from.id})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local flag = data.from == player and "e" or "he"
    local card = room:askToChooseCard(player, {
      target = data.from,
      flag = flag,
      skill_name = skel.name,
    })
    room:obtainCard(player, card, false, fk.ReasonPrey, player, skel.name)
  end
})

return skel
